package com.ovopark.firecar.adapter

import android.bluetooth.BluetoothDevice
import android.content.Context
import android.util.Log
import android.widget.LinearLayout
import android.widget.TextView
import android.widget.Toast
import cn.yhq.dialog.core.DialogBuilder
import cn.yhq.dialog.core.IDialog
import com.huashi.bluetooth.HSBlueApi
import com.ovopark.firecar.R
import com.ovopark.firecar.recyclerview.adapter.KingRecyclerViewAdapter
import com.ovopark.firecar.recyclerview.delegate.ItemViewDelegate
import com.ovopark.firecar.recyclerview.viewholder.BaseRecyclerViewHolder

class BluetoothDeviceAdapter(var mContext : Context,var api : HSBlueApi,var callback : DeviceCallback) : KingRecyclerViewAdapter<BluetoothDevice>(mContext) {
    var TAG : String = "BluetoothDeviceAdapter"
    lateinit var tvDeviceName : TextView
    lateinit var tvDeviceMac : TextView
    lateinit var llDevicConnect : LinearLayout
    var isCon : Boolean = false
    interface DeviceCallback{
        fun connectState(boolean: Boolean)
    }
    init {
        addItemViewDelegate(object : ItemViewDelegate<BluetoothDevice>{
            override fun getItemViewLayoutId(): Int = R.layout.item_bluetooth_device

            override fun isForViewType(item: BluetoothDevice?, position: Int): Boolean = true

            override fun convert(
                holder: BaseRecyclerViewHolder?,
                t: BluetoothDevice,
                position: Int
            ) {
                tvDeviceName = holder!!.getView(R.id.tv_device_name)
                tvDeviceMac = holder.getView(R.id.tv_device_mac)
                llDevicConnect =holder.getView(R.id.ll_device_connect)
                tvDeviceName.text = t.name
                tvDeviceMac.text = t.address
                llDevicConnect.setOnClickListener {
                    var bluetoothState : Int  = -1
                    bluetoothState = api.connect(t.address)
                    if (bluetoothState==0){
                        Toast.makeText(mContext,"连接成功",Toast.LENGTH_SHORT).show()
                        isCon = true
                        callback.connectState(isCon)
                    }else{
                        Toast.makeText(mContext,"连接失败",Toast.LENGTH_SHORT).show()
                        api.init()
                        callback.connectState(isCon)
                    }
                }

            }

        })
    }
}